<?php
include("/usr/share/jpgraph/jpgraph.php");
include("/usr/share/jpgraph/jpgraph_bar.php");

require_once("config.php");
require_once("CMySQL.php");

define("NB_ANNEE", 3);

$graph=new Graph(800, 600, "auto");
$graph->title->Set("Devis mois/mois");
$graph->title->SetFont(FF_VERDANA,FS_NORMAL,12);
$graph->SetScale("textlin");
$graph->yscale->ticks->Set(10, 5);
$graph->xaxis->SetTickLabels(array("Janvier", "Février", "Mars", "Avril", "Mai", "Juin", "Juillet", "Août", "Septembre", "Octobre", "Novembre", "Décembre"));
$graph->xaxis->SetFont(FF_VERDANA,FS_NORMAL,10);
$graph->xaxis->SetLabelAngle(50);
$graph->yaxis->SetFont(FF_VERDANA,FS_NORMAL,10);
$graph->legend->SetFont(FF_VERDANA,FS_NORMAL,10);
$graph->legend->pos(0.005, 0.005);

$dbName="";
if(isset($_GET["dossier"])) {
	$dbName=CMySQL::getInstance()->getDbNameFromDossier($_GET["dossier"]);
}

$sql="SELECT DATE_FORMAT(creation, \"%Y-%m\") as periode, ";
$sql.="sum(IF(status=\"ABO\", quantite*prixUnitaireHT, 0)) as mntAbo, ";
$sql.="sum(IF(status=\"CUR\", quantite*prixUnitaireHT, 0)) as mntCur, ";
$sql.="sum(IF(status=\"NAB\", quantite*prixUnitaireHT, 0)) as mntNab ";
$sql.="FROM teteDevis INNER JOIN ligneDevis ON devis=code ";
$sql.="WHERE YEAR(creation) >= YEAR(DATE_SUB(CURDATE(), INTERVAL ".(NB_ANNEE-1)." YEAR)) AND stat='OUI'";
$sql.="GROUP BY periode ";
$sql.="ORDER by periode";

CMySQL::getInstance()->selectDb($dbName);

$result=CMySQL::getInstance()->execQuery($sql);

date_default_timezone_set("Europe/Paris");
$today=getdate();
$periodeMax=$today["year"]."-12";
$data=array();
$row=mysql_fetch_array($result);
$annee=$today["year"]-NB_ANNEE+1;
$mois=1;
$bplotsAbo=array();
$bplotsCur=array();
$bplotsNab=array();
$accBarPlot=array();
$color=array("blue", "orange", "yellow");
$idAnnee=0;
do
{
	$periode=$annee."-".str_pad($mois, 2, "0", STR_PAD_LEFT);
	$valueAbo=0;
	$valueCur=0;
	$valueNab=0;
	if($row)
	{
		if($periode == $row["periode"])
		{
			$valueAbo=$row["mntAbo"]*1;
			$valueCur=$row["mntCur"]*1;
			$valueNab=$row["mntNab"]*1;
			$row=mysql_fetch_array($result);
		}	
	}
	$dataAbo[$idAnnee][$mois-1]=$valueAbo;
	$dataCur[$idAnnee][$mois-1]=$valueCur;
	$dataNab[$idAnnee][$mois-1]=$valueNab;

	$mois++;
	if($mois == 13)
	{
		$bplotsAbo[$idAnnee]=new BarPlot($dataAbo[$idAnnee]);
		$bplotsAbo[$idAnnee]->SetFillColor($color[$idAnnee]);
		$bplotsAbo[$idAnnee]->SetLegend("Devis aboutis année $annee");
		
		$bplotsCur[$idAnnee]=new BarPlot($dataCur[$idAnnee]);
		$bplotsCur[$idAnnee]->SetFillColor("green");
		
		$bplotsNab[$idAnnee]=new BarPlot($dataNab[$idAnnee]);
		$bplotsNab[$idAnnee]->SetFillColor("red");
		
		$accBarPlot[$idAnnee] = new AccBarPlot(array($bplotsAbo[$idAnnee], $bplotsCur[$idAnnee], $bplotsNab[$idAnnee]));

		$idAnnee++;
		$annee++;
		$mois=1;
	}
}while($periode < $periodeMax);

$bplotsCur[NB_ANNEE-1]->SetLegend("Devis en cour");
$bplotsNab[NB_ANNEE-1]->SetLegend("Devis non aboutis");
$gbarplot=new GroupBarPlot($accBarPlot);

$graph->Add($gbarplot);

/*echo "<pre>";
print_r($data);
echo "</pre>";*/

$graph->stroke();
?>
